package org.yzz.auth.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yzz.auth.annotation.RonghuaPermission;
import org.yzz.auth.domain.Menu;
import org.yzz.auth.query.MenuQuery;
import org.yzz.auth.service.IMenuService;
import org.yzz.base.utils.AjaxResult;
import org.yzz.base.utils.PageInfo;

import java.util.List;
import java.util.Objects;

@RequestMapping("/menu")
@RestController
@RonghuaPermission(name = "菜单管理")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @GetMapping
    @RonghuaPermission(name = "查询全部菜单")
    @ApiOperation(value = "查询全部菜单" )
    public AjaxResult selectAll(){
        try {
            List<Menu> menus = menuService.selectAll();
            return AjaxResult.success(menus);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @RonghuaPermission(name = "通过ID查询菜单")
    @ApiOperation(value = "通过ID查询菜单" )
    public AjaxResult selectById(@PathVariable Long id){
        try {
            Menu menu = menuService.selectById(id);
            return AjaxResult.success(menu);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增或修改
     */
    @PostMapping
    @RonghuaPermission(name = "菜单新增或修改")
    @ApiOperation(value = "新增或修改" )
    public AjaxResult save(@RequestBody Menu menu){
        try {
            if(Objects.nonNull(menu.getId())){
                menuService.update(menu);
                return AjaxResult.success();
            }
            menuService.insert(menu);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @RonghuaPermission(name = "菜单根据指定 id 删除")
    @ApiOperation(value = "删除" )
    public AjaxResult delete(@PathVariable Long id){
        try {
            menuService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param menuQuery
     * @return
     */
    @PutMapping
    @RonghuaPermission(name = "菜单分页查询")
    public AjaxResult queryPage(@RequestBody MenuQuery menuQuery){
        try {
            PageInfo<Menu> pageInfo = menuService.queryPage(menuQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PatchMapping
    @RonghuaPermission(name = "菜单批量删除")
    public  AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            menuService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/menuTree")
    @RonghuaPermission(name = "查询菜单树")
    public AjaxResult deptTree(){
        try {
            List<Menu> list = menuService.menuTree();
            return AjaxResult.success(list);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据员工 id 查询菜单
     * @return
     */
    @GetMapping("/menuTree/{id}")
    public AjaxResult queryMenuTreeByUserId(@PathVariable Long id){
        try {
            List<Menu> list = menuService.queryMenuTreeByUserId(id);
            return AjaxResult.success(list);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/menuParent")
    @RonghuaPermission(name = "查询父菜单")
    public AjaxResult getMenuParent(){
        try {
            List<Menu> list = menuService.getMenuParent();
            return AjaxResult.success(list);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
